<?php
/**
 * Copyright (c) 2006-2007, Julien PORTALIER
 * 
 * Derived from CakePHP's TimeHelper Copyright (c) 2006,
 * Cake Software Foundation, Inc.
 * @link http://cakephp.org/
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright    Copyright (c) 2006-2007, Julien PORTALIER
 * @link         http://featherphp.googlecode.com/
 * @license      http://www.opensource.org/licenses/mit-license.php The MIT License
 */

/**
 * Useful functions to manipulate dates.
 */
class Time
{
	static function fromString($date_string)
	{
		return is_integer($date_string) ? $date_string : strtotime($date_string);
	}
	
	static function date($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string) : time();
		return strftime(_("%Y-%m-%d"), $date);
	}
	
	static function datetime($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string) : time();
		return strftime(_("%Y-%m-%d %H:%M"), $date);
	}
	
	static function niceDate($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string) : time();
		if (self::isThisYear($date_string))
			return ucfirst(strftime(_("%b %e"), $date));
		return ucfirst(strftime(_("%b %e, %Y"), $date));
	}
	
	static function nice($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string) : time();
		return strftime(_("%A %B %e %Y, %I:%M%p"), $date);
	}
	
	static function niceShort($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string) : time();
		
		if (self::isToday($date))
			return sprintf(_('today, %s'), date("H:i", $date));
		elseif (self::wasYesterday($date))
			return sprintf(_('yesterday, %s'), date( "H:i", $date));
		else
			return strftime(self::isThisYear($date) ? _("%B %e, %I:%M%p") : _("%B %e %Y, %I:%M%p"), $date);
	}
	
	static function short($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string): time();
		$ret = strftime(self::isThisYear($date) ? _("%b %e, %I:%M%p") : _("%b %e %Y, %I:%M%p"), $date);
		return $ret;
	}
	
	static function isInFuture($string)
	{
		return (self::fromString($string) > time());
	}
	
	static function isToday($date_string)
	{
		return date('Y-m-d', self::fromString($date_string)) == date( 'Y-m-d', time());
	}	
	
	static function isThisYear($date_string)
	{
		return date('Y', self::fromString($date_string)) == date( 'Y', time());
	}
	
	static function wasYesterday($date_string)
	{
		return date('Y-m-d', self::fromString($date_string)) == date( 'Y-m-d', strtotime('yesterday'));
	}
	
	static function isTomorrow($date_string)
	{
		return date('Y-m-d', self::fromString($date_string)) == date('Y-m-d', strtotime('tomorrow'));
	}
	
	static function toAtom($date_string)
	{
		return date('Y-m-d\TH:i:s\Z', self::fromString($date_string));
	}
	
	static function toRSS($date_string)
	{
		return date('r', self::fromString($date_string));
	}
	
	static function SQL($date_string=null)
	{
		$date = $date_string ? self::fromString($date_string) : time();
		return date('Y-m-d H:i:s', self::fromString($date));
	}
	
	static function ago($date_string)
	{
		/*
		$date = $date_string ? self::fromString($date_string) : time();
		$now  = time();
		if ($date > $now)
			return self::datetime($date);
		elseif ($now - $date < 60)
			return _('a minute ago');
		elseif ($now - $date < 3600)
		{
			$minutes = round(($now - $date) / 60.0);
			return sprintf(ngettext('a minute ago', '%d minutes ago', $minutes), $minutes);
		}
#		elseif ($now - $date < 3600 * 24)
		elseif (($now - $date < 3600 * 6) or self::isToday($date))
		{
			$hours = round(($now - $date) / 3600.0);
			return sprintf(ngettext('an hour ago', '%d hours ago', $hours), $hours);
		}
		elseif (self::wasYesterday($date_string))
		{
			$days = round(($now - $date) / (3600 * 24.0));
			return sprintf(ngettext('yesterday', '%d days ago', $days), $days).' '.strftime(_("%I:%M%p"), $date);
		}
		return self::niceDate($date);
		*/
		$date     = self::fromString($date_string);
		$diff     = time() - $date;
		$day_diff = round($diff / 86400);
		
		if ($day_diff == 0)
		{
			if     ($diff < 60)    return _('just now');
			elseif ($diff < 120)   return _('a minute ago');
			elseif ($diff < 3600)  return sprintf(_('%d minutes ago'), round($diff / 60));
			elseif ($diff < 7200)  return _('an hour ago');
			elseif ($diff < 86400) return sprintf(_('%d hours ago'), round($diff / 3600));
		}
		elseif ($day_diff == 1) {
			return _('Yesterday');
		}
		elseif ($day_diff < 7) {
			return sprintf(_('%d days ago'), $day_diff);
		}
		elseif ($day_diff < 31) {
			return sprintf(_('%d weeks ago'), ceil($day_diff / 7));
		}
		return self::niceDate($date);
	}
}
?>